home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-11-20 | 9.3 KB | 369 lines | [TEXT/MPS ] |
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Controls.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <Traps.h>
- #include <StdLib.h>
-
- #include "TDocument.h"
- #include "TApplication.h"
- #include "MacTutorApp.h"
- #include "MacTutorDoc.h"
- #include "MacTutorGrow.h"
-
- // constants used for scroll bar and grow box size adjustment
- const short kScrollbarAdjust = 15;
- const short kGrowboxAdjust = 15;
- const short kScrollbarWidth = 16;
- const short kScrollTweek = 2;
- const short kControlVisible = 0xFF;
-
- extern "C" {
- // prototypes for functions that don't belong to any one class
- void CommonAction(ControlHandle control,short* amount);
- pascal void VActionProc(ControlHandle control,short part);
- pascal void HActionProc(ControlHandle control,short part);
- };
-
- // methods for the MacTutorGrow class
-
- // create and delete document windows
- // override methods from MacTutorDocument
- // The base class creates the window and sets the display string.
- // We try to get the controls, and display an error if we can't
- //
- TMacTutorGrow::TMacTutorGrow(short resID, StringPtr s) : (resID,s)
- {
- Boolean good;
- fDocVScroll = GetNewControl(rVScroll, fDocWindow);
- good = (fDocVScroll != nil);
- if ( good)
- {
- fDocHScroll = GetNewControl(rHScroll, fDocWindow);
- good = (fDocHScroll != nil);
- }
- if ( good ) // good? — adjust & draw the controls
- {
- AdjustScrollValues(true);
- }
- else
- {
- // tell user we failed
- HideWindow(fDocWindow);
- AlertUser(kErrStrings,eNoWindow);
- }
- }
-
- TMacTutorGrow::~TMacTutorGrow(void)
- {
- HideWindow(fDocWindow);
- if ( fDocVScroll != nil )
- { DisposeControl(fDocVScroll); }
- if ( fDocHScroll != nil )
- { DisposeControl(fDocHScroll); }
- }
-
- void TMacTutorGrow::DoUpdate(void)
- {
-
- BeginUpdate(fDocWindow);
- if ( ! EmptyRgn(fDocWindow->visRgn) )
- {
- DrawWindow();
- }
- EndUpdate(fDocWindow);
- }
-
- void TMacTutorGrow::DrawWindow(void)
- {
- Rect tRect;
- SetPort(fDocWindow);
- tRect = fDocWindow->portRect;
- tRect.bottom = tRect.bottom - kScrollbarAdjust;
- tRect.right = tRect.right - kScrollbarAdjust;
- EraseRect(&tRect);
-
- MoveTo(100,100);
- TextSize(18); TextFont(monaco);
- DrawString(fDisplayString);
- DrawControls(fDocWindow);
- DrawGrowIcon(fDocWindow);
-
- } // DrawWindow
-
- void TMacTutorGrow::DoActivate(Boolean becomingActive)
- {
- if ( becomingActive )
- {
- Rect growRect;
- Rect tRect;
-
- /* the controls must be redrawn on activation: */
- (*fDocVScroll)->contrlVis = kControlVisible;
- (*fDocHScroll)->contrlVis = kControlVisible;
- // copy rectangles to avoid unsafe object field references!
- tRect = (*fDocVScroll)->contrlRect; InvalRect(&tRect);
- tRect = (*fDocHScroll)->contrlRect; InvalRect(&tRect);
- // the growbox needs to be redrawn on activation:
- growRect = fDocWindow->portRect;
- // adjust for the scrollbars
- growRect.top = growRect.bottom - kScrollbarAdjust;
- growRect.left = growRect.right - kScrollbarAdjust;
- InvalRect(&growRect);
- }
- else
- {
- /* the controls must be hidden on deactivation: */
- HideControl(fDocVScroll);
- HideControl(fDocHScroll);
- // we draw grow icon immediately, since we deactivate controls
- // immediately, and the update delay looks funny
- DrawGrowIcon(fDocWindow);
- }
- }
-
- void TMacTutorGrow::AdjustScrollSizes(void)
- {
- MoveControl(fDocVScroll,
- fDocWindow->portRect.right - kScrollbarAdjust,
- fDocWindow->portRect.top);
- SizeControl(fDocVScroll, kScrollbarWidth,
- fDocWindow->portRect.bottom - fDocWindow->portRect.top -
- kGrowboxAdjust + kScrollTweek);
- MoveControl(fDocHScroll,
- fDocWindow->portRect.left,
- fDocWindow->portRect.bottom - kScrollbarAdjust);
- SizeControl(fDocHScroll,
- fDocWindow->portRect.right - fDocWindow->portRect.left -
- kGrowboxAdjust + kScrollTweek,
- kScrollbarWidth);
- } // AdjustScrollSizes
-
- void TMacTutorGrow::AdjustScrollbars(Boolean needsResize)
- {
- (*fDocVScroll)->contrlVis = 0;
- (*fDocHScroll)->contrlVis = 0;
- if ( needsResize ) AdjustScrollSizes();
- AdjustScrollValues(needsResize);
- // restore visibility in case we never had to draw during adjustment
- (*fDocVScroll)->contrlVis = 0xff;
- (*fDocHScroll)->contrlVis = 0xff;
- } // AdjustScrollbars
-
-
- void TMacTutorGrow::AdjustScrollValues(Boolean mustRedraw)
- {
- SetCtlMin(fDocVScroll,0);
- SetCtlMax(fDocVScroll,1000);
- SetCtlValue(fDocVScroll,500);
- SetCtlMin(fDocHScroll,0);
- SetCtlMax(fDocHScroll,1000);
- SetCtlValue(fDocHScroll,500);
- if ( mustRedraw )
- {
- ShowControl(fDocVScroll);
- ShowControl(fDocHScroll);
- }
- } // AdjustScrollValues
-
- void TMacTutorGrow::DoZoom(short partCode)
- {
- Rect tRect;
-
- tRect = fDocWindow->portRect;
- EraseRect(&tRect);
- ZoomWindow(fDocWindow, partCode, fDocWindow == FrontWindow());
- AdjustScrollbars(true); // adjust, redraw anyway
- InvalRect(&tRect); // invalidate the whole content
- // the scrollbars were taken care of by AdjustScrollbars, so validate ’em
- tRect = (*fDocVScroll)->contrlRect;
- ValidRect(&tRect);
- tRect = (*fDocHScroll)->contrlRect;
- ValidRect(&tRect);
- }
-
- void TMacTutorGrow::DoGrow(EventRecord* theEvent)
- {
- long growResult;
- Rect tRect, tRect2;
-
- tRect = qd.screenBits.bounds;
- tRect.left = kMinDocDim;
- tRect.top = kMinDocDim;
- tRect2 = fDocWindow->portRect;
- tRect2.bottom = tRect2.bottom - kScrollbarAdjust;
- tRect2.right = tRect2.right - kScrollbarAdjust;
- growResult = GrowWindow(fDocWindow, theEvent->where, &tRect);
- // see if it really changed size
- if ( growResult != 0 )
- {
- SizeWindow(fDocWindow, LoWrd(growResult), HiWrd(growResult), true);
- AdjustScrollbars(true);
- // calculate & validate the region that hasn’t changed so it won’t get redrawn
- // Note: we copy rectangles so that we don't take address of object fields.
- tRect = fDocWindow->portRect;
- (void) SectRect(&tRect, &tRect2, &tRect2);
- InvalRect(&tRect); ValidRect(&tRect2);
- tRect2 = (*fDocVScroll)->contrlRect; ValidRect(&tRect2);
- tRect2 = (*fDocHScroll)->contrlRect; ValidRect(&tRect2);
- // redraw grow icon
- tRect.top = tRect.bottom - kScrollbarAdjust;
- tRect.left = tRect.right - kScrollbarAdjust;
- InvalRect(&tRect);
- }
- }
-
- void TMacTutorGrow::DoContent(EventRecord* theEvent)
- {
- Point mouse;
- ControlHandle control;
- short part, value;
- Rect tRect;
-
- SetPort(fDocWindow);
- mouse = theEvent->where;
- GlobalToLocal(&mouse);
-
- tRect = fDocWindow->portRect;
- tRect.bottom = tRect.bottom - kScrollbarAdjust;
- tRect.right = tRect.right - kScrollbarAdjust;
- part = FindControl(mouse, fDocWindow, &control);
- switch ( part )
- {
- case 0: break;
- case inThumb:
- value = GetCtlValue(control);
- part = TrackControl(control, mouse, nil);
- if ( part != 0 )
- {
- value -= GetCtlValue(control);
- // value now has CHANGE in value; if value changed, scroll
- if ( value != 0 )
- {
- if ( control == fDocVScroll )
- {
- ScrollRect (&tRect, 0, value, nil);
- SetOrigin(tRect.left,tRect.top-value);
- }
- else
- {
- ScrollRect (&tRect, value, 0, nil);
- SetOrigin(tRect.left-value,tRect.top);
- }
- AdjustScrollSizes();
- }
- }
- break;
- default: // they clicked in an arrow, so track & scroll
- if ( control == fDocVScroll )
- value = TrackControl(control, mouse, (ProcPtr) VActionProc);
- else value = TrackControl(control, mouse, (ProcPtr) HActionProc);
- AdjustScrollSizes();
- break;
- }
- }
-
- // Common algorithm for pinning the value of a control. It returns the actual amount
- // the value of the control changed.
-
- void CommonAction(ControlHandle control,short* amount)
- {
- short value, max;
-
- value = GetCtlValue(control);
- max = GetCtlMax(control);
- *amount = value - *amount;
- if ( *amount <= 0 )
- *amount = 0;
- else if ( *amount >= max )
- *amount = max;
- SetCtlValue(control, *amount);
- *amount = value - *amount;
- } // CommonAction
-
-
- pascal void VActionProc(ControlHandle control,short part)
- {
- short amount;
- WindowPtr window;
- Rect tRect;
-
- if ( part != 0 )
- {
- window = (*control)->contrlOwner;
- tRect = window->portRect;
- tRect.bottom = tRect.bottom - kScrollbarAdjust;
- tRect.right = tRect.right - kScrollbarAdjust;
- switch ( part )
- {
- case inUpButton:
- case inDownButton: // 5 pixels
- amount = 5;
- break;
- case inPageUp: // 50 pixels
- case inPageDown:
- amount = 50;
- break;
- }
- if ( (part == inDownButton) || (part == inPageDown) )
- amount = -amount; // reverse direction
- CommonAction(control, &amount);
- if ( amount != 0 )
- {
- ScrollRect (&tRect, 0, amount, nil);
- SetOrigin(tRect.left,tRect.top-amount);
- }
- }
- } // VActionProc
-
- // Determines how much to change the value of the horizontal scrollbar by and how
- // much to scroll the TE record.
-
- pascal void HActionProc(ControlHandle control,short part)
- {
- short amount;
- WindowPtr window;
- Rect tRect;
-
- if ( part != 0 )
- {
- window = (*control)->contrlOwner;
- tRect = window->portRect;
- tRect.bottom = tRect.bottom - kScrollbarAdjust;
- tRect.right = tRect.right - kScrollbarAdjust;
- switch ( part )
- {
- case inUpButton:
- case inDownButton: // 5 pixels
- amount = 5;
- break;
- case inPageUp: // 50 pixels
- case inPageDown:
- amount = 50;
- break;
- }
- if ( (part == inDownButton) || (part == inPageDown) )
- amount = -amount; // reverse direction
- CommonAction(control, &amount);
- if ( amount != 0 )
- {
- ScrollRect (&tRect, amount, 0, nil);
- SetOrigin(tRect.left-amount,tRect.top);
- }
- }
- } // HActionProc
-
-